package com.metalgun.gui;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.metalgun.thread.Mythread;

public class Myframe extends JFrame{
	private JLabel label;
	private JLabel label1;
	private JPanel panel;
	private JPanel panel1;
	private JButton button1;
	private JButton button2;
	private JTextField field;
	private JTextField fieldServ;
	private JTextField fieldPort;
	private JFileChooser fileChooser ;
	
	public Myframe(){
		super();
		label1 = new JLabel("Serveur :");
		fieldServ = new JTextField();
		fieldServ.setColumns(8);
		fieldPort = new JTextField();
		fieldPort.setColumns(4);
		panel1 = new JPanel();
		label = new JLabel("Choisir le fichier :");
		setTitle("Client");
		panel = new JPanel(new BorderLayout());
		panel.add(label, BorderLayout.NORTH);
		field = new JTextField();
		field.setColumns(17);
		panel.add(field,BorderLayout.WEST);
		panel1.add(label1);
		panel1.add(fieldServ);
		panel1.add(fieldPort);
		panel.add(panel1, BorderLayout.NORTH);
		button1 = new JButton("Parcourir");
		button1.addActionListener(
				new java.awt.event.ActionListener() {
					public void actionPerformed(
						java.awt.event.ActionEvent e) {
						fileChooser = new JFileChooser();
						fileChooser.setMultiSelectionEnabled(false);
						fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 
						fileChooser.showOpenDialog(null);
						System.out.println(fileChooser.getSelectedFile().getAbsolutePath());
						field.setText(fileChooser.getSelectedFile().getAbsolutePath());
						repaint();
					}
				}
			);
		button2 = new JButton("Send");
		button2.addActionListener(
				new java.awt.event.ActionListener() {
					public void actionPerformed(
						java.awt.event.ActionEvent e) {
						Mythread thread = new Mythread(field.getText(),fieldServ.getText(),new Integer(fieldPort.getText()));
						thread.start();
						repaint();
					}
				}
			);
		panel.add(button1,BorderLayout.EAST);
		panel.add(button2, BorderLayout.SOUTH);
		setContentPane(panel);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setResizable(false);
		setVisible(true);
		setSize(300, 100);
	}
}
